<?php

class Client_Table_Categories extends Apk_Db_Table
{
	protected $_name      = 'categories';
    protected $_primary   = 'id';

    protected $_dependentTables = array('Client_Table_Rule', 'Client_Table_Categories', 'Client_Table_Privilege');

	protected $_referenceMap = array('Parent' => array(
            								            'columns'       => 'parent_id',
            								            'refTableClass' => 'Client_Table_Categories',
            								            'refColumns'    => 'id'));
    protected $_ERROR = '';
    protected $_lang;

    public function __construct($lang = 'ru')
    {
    	$this->_lang = $lang;
    	parent::__construct();
    }

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetResources()
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('is_active')
    				->order('l');

    	return $this->getList($sel)->toArray();
    }


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				//->from($this->_name, array('id', 'title' => 'title_'.$this->_lang, 'text' => 'text_'.$this->_lang, 'path', 'file_name', 'is_show_gallery', 'ceo_desc', 'ceo_title', 'ceo_keywords'))
    				->where('is_active');

        if(intval($id) > 0) $sel->where($this->_name.'.id = ?', $id);
		else $sel->where($this->_name.'.idt = ?', $id);

    	try {
	    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
		}
		catch (Exception $e)
		{
			return array();
		}

		return $data;
    }

    public function GetStatic($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name, array('id', 'title' => 'title_'.$this->_lang, 'desc' => 'desc_'.$this->_lang, 'text' => 'text_'.$this->_lang, 'ceo' => 'ceo_'.$this->_lang, 'path', 'file_name', 'is_show_gallery', 'level', 'ceo_title_ua', 'ceo_title', 'ceo_keywords_ua', 'ceo_keywords', 'ceo_desc_ua', 'ceo_desc'))
    				->where('is_active')
    				->where('id = ?', $id);


    	try{
    		$data =  $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    	}catch(Exception $e)
    	{
    		echo $e->__toString(); die();
    	}

    	return $data;
    }

    public function GetPhotos($id)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from('objects')
    		  ->where('id_cat = ?', $id)
    		  ->where('is_ghost = 0')
    		  ->where('is_removed = 0')
    		  ->where('is_active')
    		  ->order('norder');

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }


    public function GetDataList()
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)
    		  ->order('l');

    	$data['data'] = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	return $data;
    }

    public function GetCatList($params = null, $limit = null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'=>'title_'.$this->_lang, 'menu_name'=>'menu_name_'.$this->_lang, 'desc'=>'desc_'.$this->_lang, 'text'=>'text_'.$this->_lang, 'level', 'url', 'parent_id', 'l', 'r', 'is_community', 'is_show', 'id_type', 'id_menu', 'idt', 'file_name', 'path'))
    		  ->where('is_active')
    		  ->order('l');

    	if($limit > 0) $sel->limit($limit);

        if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    		    case 'parent':
                    $cat = $this->GetDataRow($val);
                    $sel->where('l > ?', $cat['l']);
                    $sel->where('r < ?', $cat['r']);
    		        break;
    		    default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

    	//echo $sel; die();

        return  $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    public function GetChildList($id, $a = array(), $with_parent = false)
    {
    	$p = $this->GetDataRow($id);

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'=>'title_'.$this->_lang, 'menu_name'=>'menu_name_'.$this->_lang, 'desc'=>'desc_'.$this->_lang, 'level', 'url', 'parent_id', 'l', 'r', 'is_community', 'is_show', 'id_type', 'id_menu', 'idt', 'path', 'file_name'))
    		  ->where('is_active')
    		  ->order('l');

		if($with_parent)
		{
			$sel->where('l >= ?', $p['l']);
			$sel->where('r <= ?', $p['r']);
		}
		else
		{
			$sel->where('l > ?', $p['l']);
			$sel->where('r < ?', $p['r']);
		}

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if(count($a) > 0 && is_array($a))
    	foreach ($data as $k => $v)
    		if(in_array($v['id'], $a)) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetChildsID($id)
    {
    	$p = $this->GetDataRow($id);

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id'))
    		  ->where('is_active')
    		  ->where('l > ?', $p['l'])
    		  ->where('r < ?', $p['r'])
    		  ->order('l');

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    public function GetChildsIDsArray($id)
    {
    	$p = $this->GetDataRow($id);

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id'))
    		  ->where('is_active')
    		  ->where('l > ?', $p['l'])
    		  ->where('r < ?', $p['r'])
    		  ->order('l');

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    	foreach($data as $v) $d[] = $v['id'];
    	return $d;
    }


	public function insert(array $data)
    {
		if(!is_array($data)) $data = array();
        return parent::insert($data);
    }

    public function update(array $data, $id)
    {
		$data['modify_date'] = time();
		return parent::update($data, 'id = '.$id);
    }

    public function remove($id)
    {
    	parent::delete('id = '.$id);
	}

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'add_date':
            case 'modify_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            case 'file_size':
                $result[$key] = round($value/1024/1024, 2).' MB';
                break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>